{extend name="./tpl/default/mobile/public/layout.html" /}
{block name="header"}
<link rel="stylesheet" type="text/css" href="/public/home/mobile/css/shopcar.css">
<script type="text/javascript" src="/public/home/mobile/js/auto-size.js"></script>
<style>
    .layui-layer-btn a{font-size:1 }
</style>
{/block}
{block name="head"}
<div class="fix_head">
    <a href="javascript:history.go(-1);" class="back">
        <img src="/public/home/mobile/images/back.jpg">
    </a>
    <p>购物车</p>
    <a href="{:url('/home/index/index')}" class="home">
        <img src="/public/home/mobile/images/home.jpg">
    </a>
</div>
<div class="sc_h"></div>
{/block}
{block name="body"}
<input type="hidden" id="skuIds" value="{$cartList.sku_ids}">
<div id="cartlist">
</div>
{/block}
{block name="foot"}
<div class="fix_bottom">
    <div id="qx" data="0"></div><span style="float:left;line-height:0.8rem;">全选</span>
    <div class="zjq">
        <p class="p1">合计:￥
            <span id="price{$shopId}" >0</span>
        </p>
    </div>
    <a id="js" onclick="orderConfirm()">结算(<span id="num">0</span>)</a>
</div>
{/block}
{block name="script"}
<script src="/public/plugins/template/template.js"></script>
<script src="/public/common.js"></script>
<script src="/public/plugins/layer/layer.js"></script>
<script>
    $(".lingqu").each(function(){
        $(this).click(function () {
            $('#pmt').fadeIn('show');
            $("#pmtbox").animate({
                height: "50vh",
            }, 300 );
        });
        $('#back').click(function () {
            $('#pmt').fadeOut('show');
            $("#pmtbox").animate({
                height: "0",
            }, 300 );
        });
    });
</script>


<script id="ajaxListContent" type="text/html">
    {{each item_list as value index}}
    <div class="lb" id="card{{ index  }}">
        <div class="check">
            <input class="weui-check" type="checkbox" checked="checked" onclick="check(this)" data-cartid="{{ value.cart_id }}" data-shop="{{ value.shop_id }}" name="sku_id" value="{{ value.sku_id }}" data-num="{{ value.num }}" data-price="{{ value.goods_total }}">
        </div>
        <img src="{{ value.goods_thumb}}" class="spt">
        <div class="mid">
            <p class="name">{{ value.fulltitle}}</p>
            <p class="color">{{ value.sku_name}}</p>
            <div class="jj">
                <span class="jia" onclick="dec(this)" data-skuid="{{ value.sku_id}}">-</span>
                <input class="num" value="{{ value.num}}">
                <span class="jian" onclick="inc(this)"  data-skuid="{{ value.sku_id}}">+</span>
            </div>
        </div>
        <div class="jgq">
            <div class="price"><p>￥<span class="pri">{{ value.simple_price}}</span></p></div>
            <div class="price_de"><p><del>￥{{ value.price}}</del></p></div>
            <img src="/public/home/mobile/images/del.jpg" class="del"  onclick="doAction(this)"
                 data-msg="确认要删除吗？"
                 data-url="{:url('delCart')}"
                 data-params="cart_id={{ value.cart_id }}">
        </div>
    </div>
    {{/each}}
</script>
<script>
    $(document).ready(function(){
        getCartList();
    });
    //获取购物车列表
    function getCartList() {
        var skuids=$('#skuIds').val()+',';
        $.post("{:url('index')}",'',function (data) {
            $('#price').text(data.total_price);
            $('#num').text(data.total_num);
            var html = template('ajaxListContent',data);
            $('#cartlist').html(html);
            $('#cartlist').find('input[name="sku_id"]').each(function (){
                var skuid=$(this).val();
                if(skuids.indexOf(skuid+",")>-1) {
                    $(this).prop("checked", true);
                }else{
                    $(this).prop("checked", false);
                }
            });
            $('.carbox').each(function () {
                var shopid=$(this).find('.settlement').data('shop');
                shoptotal(shopid);
            })
        })
    }
    //全选
    function checkAll(obj) {
        var shopid=$(obj).data('shop');
        var skuids=$('#skuIds').val();
        $('#cart'+shopid).find('input[type="checkbox"]').each(function (){
            $(this).prop("checked", true);
            var skuid=$(this).val();
            if(skuids.indexOf(skuid+",")<=-1) {
                skuids=skuids+skuid+',';
                $('#skuIds').val(skuids);
            }
        });
        shoptotal(shopid);
    }
    //单选
    function check(obj) {
        var shopid=$(obj).data('shop');
        var skuids=$('#skuIds').val();
        var skuid=$(obj).val();
        if($(obj).is(':checked')){
            if(skuids.indexOf(skuid+",")<=-1) {
                skuids=skuids+skuid+',';
            }
        }else{
            if(skuids.indexOf(skuid+",")>-1) {
                skuids=skuids.replace(skuid+',',"");
            }
        }
        $('#skuIds').val(skuids);
        shoptotal(shopid);
    }
    //计算店铺商品价格及数量
    function shoptotal(id){
        var site_total_num=0;
        var site_total_price=0;
        $('#cart'+id).find('input[name="sku_id"]').each(function (){
            if($(this).is(':checked')) {
                var num=$(this).data('num');
                var price=$(this).data('price');
                site_total_num+=parseFloat(num);
                site_total_price+=parseFloat(price);
            }
        });
        $('#num'+id).empty().text(site_total_num);
        $('#price'+id).empty().text(site_total_price.toFixed(4));
    }
    //减
    function dec(obj){
        var num=$(obj).parent().find('input').val();
        var skuid=$(obj).data('skuid');
        changeCart((parseInt(num)-1),skuid);
    }
    //加
    function inc(obj){
        var num=$(obj).parent().find('input').val();
        var skuid=$(obj).data('skuid');
        changeCart((parseInt(num)+1),skuid);
    }
    function numchange(obj) {
        var num=  parseInt($(obj).val());
        var skuid=parseInt($(obj).data('skuid'));
        var num2=parseInt($(obj).data('num'));
        if(!isNaN(num)){
            changeCart(num,skuid);
        }else{
            layer.msg('购买数量必须为数字！');
            changeCart(num2,skuid);
        }
    }
    //加减时改变当前复选框为选中
    function checkthis(skuid){
        var skuids=$('#skuIds').val();
        var skuid=skuid;
        var obj=$('input[value='+skuid+']');
        obj.prop("checked", true);
        if(skuids.indexOf(skuid+",")<=-1) {
            skuids=skuids+skuid+',';
            $('#skuIds').val(skuids);
        }
    }
    //刷新购物车
    function changeCart(num,skuid){
        checkthis(skuid);
        if(num==0){
            var skuids=$('#skuIds').val();
            skuids=skuids.replace(skuid+',',"");
            $('#skuIds').val(skuids);
        }
        $.post("{:url('addCart')}",{sku_id:skuid,number:num},function (data) {
            if(data.code==0){
                layer.msg(data.msg);
                getCartList();
            }else{
                getCartList();
            }
        })
    }
    //删除
    function delall(obj) {
        var shopid=$(obj).data('shop');
        layer.confirm('确认批量删除购物车吗？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            var cart_id = new Array()
            $('#cart'+shopid).find('input[name="sku_id"]').each(function (index){
                if($(this).is(':checked')) {
                    cart_id[index1]=$(this).data('cartid');
                }
            });
            $.post("{:url('delCart')}",{cart_id:cart_id},function (data) {
                layer.msg(data.msg);
                getCartList();
            })
        }, function(){
            layer.closeAll();
        });
    }
    //结算
    function orderConfirm() {
        var skuIds = '';
        $('#cartlist').find('input[name="sku_id"]').each(function (index){
            if($(this).is(':checked')) {
                var skuId=$(this).val();
                skuIds += skuId+',';
            }
        });
        console.log(skuIds);
        skuIds = skuIds.substr(0, skuIds.length - 1);
        if(skuIds==''){
            layer.alert('请选择要提交得商品！');
        }else{
            location.href="{:url('home/Cart/orderConfirm')}?sku_ids="+skuIds;
        }
    }
</script>
{/block}
